package com.javasm.service.goods.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.goods.GoodsToGiftsCriteria;
import com.javasm.domin.entity.goods.GiftsToGoods;
import com.javasm.domin.entity.goods.GoodsToGifts;
import com.javasm.domin.vo.goods.GiftsToGoodsVo;
import com.javasm.domin.vo.goods.GoodsToGiftsVo;
import com.javasm.mapper.goods.GoodsToGiftsMapper;
import com.javasm.service.base.impl.BaseServiceimpl;
import com.javasm.service.goods.IGoodsToGiftsService;
import com.javasm.transfer.goods.GoodsToGiftsTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whl
 * @since 2022-01-16
 */
@Service
@Transactional
@RequiredArgsConstructor
public class GoodsToGiftsServiceImpl extends BaseServiceimpl<GoodsToGifts> implements IGoodsToGiftsService {

    private final GoodsToGiftsTransfer goodsToGiftsTransfer;
    private final GoodsToGiftsMapper goodsToGiftsMapper;
    @Override
    public PageResult<GoodsToGiftsVo> searchPage(GoodsToGiftsCriteria goodsToGiftsCriteria) {
        PageHelper.startPage(goodsToGiftsCriteria.getCurrentPage(), goodsToGiftsCriteria.getPageSize());
        LambdaQueryWrapper<GoodsToGifts> lambda = new QueryWrapper<GoodsToGifts>().lambda();

        if (!StringUtils.isEmpty(goodsToGiftsCriteria.getGoodsName())) {
            lambda.like(GoodsToGifts::getGoodsName, goodsToGiftsCriteria.getGoodsName());
        }
        if (!StringUtils.isEmpty(goodsToGiftsCriteria.getApproveStatus())) {
            lambda.eq(GoodsToGifts::getApproveStatus, goodsToGiftsCriteria.getApproveStatus());
        }
        if (!StringUtils.isEmpty(goodsToGiftsCriteria.getGiftsColor())) {
            lambda.like(GoodsToGifts::getGiftsColor, goodsToGiftsCriteria.getGiftsColor());
        }
        if (!StringUtils.isEmpty(goodsToGiftsCriteria.getProductId())) {
            lambda.like(GoodsToGifts::getProductId, goodsToGiftsCriteria.getProductId());
        }
        List<GoodsToGifts> goodsToGifts = goodsToGiftsMapper.selectList(lambda);
        PageInfo<GoodsToGifts> pageInfo = new PageInfo<>(goodsToGifts);
        List<GoodsToGiftsVo> goodsToGiftsVos = goodsToGiftsTransfer.setInfo(goodsToGifts);
        return new PageResult<GoodsToGiftsVo>(pageInfo.getTotal(), goodsToGiftsVos);
    }
}
